<?
	//include ("./scripts/globals.inc.php"); // Ouvrir la connexion à la bdd
	include ("./scripts/fpdf/fpdf.php");

	class lPDF extends FPDF
	{
		var $color_background_title = "#AAAAAA";
		var $color_background = "#EFEFEF";
		var $color_subtitle = "#99CC67";
		var $color = "#555555";
		
		var $posXRef = 0;
		var $posYRef = 0;
		
		function Footer()
		{
		    //Positionnement à 1,5 cm du bas
		    $this->SetY(-20);


		    $this->SetFont('Arial', '', 8);

		    $this->Cell(0, 10, utf8_decode('Tél.: 01.64.37.38.38'), 0, 0, 'R');
				$this->SetXY(10, $this->GetY() + 4);
		    $this->Cell(0, 10, 'Fax : 01.64.37.88.89', 0, 0, 'R');
				$this->SetXY(10, $this->GetY() + 4);

		    //Police Arial italique 8
		    $this->SetFont('Arial', 'I', 8);

		    // Date impression
		    $this->Cell(50, 10, utf8_decode('Généré le '.date("d/m/Y").' à '.date("H:i:s").''), 0, 0, 'L');
				
				$this->SetXY(10, $this->GetY() - 4);
		    $this->Cell(0, 10, utf8_decode('rue Claude Bernard -- 77000 La Rochette'), 0, 0, 'C');
				$this->SetXY(10, $this->GetY() + 4);
		    $this->Cell(0, 10, utf8_decode('Melun Val de Seine Entrepreneurs'), 0, 0, 'C');


		    // Adresse web
				$this->SetX(10);
		    $this->Cell(0, 10, 'http://www.melun-valdeseine-entrepreneurs.fr', 0, 0, 'R');
		}
		


		
		function SetColor($color, $color_subtitle, $color_background, $color_background_title)
		{
			if ($color_background_title != "#")		$this->color_background_title = $color_background_title;
			if ($color_background != "#")					$this->color_background 			= $color_background;
			if ($color_subtitle != "#")						$this->color_subtitle					= $color_subtitle;
			if ($color != "#")										$this->color									= $color;
		}
		
		function RenvoyerComposante($hexa)
		{
			$c[] = hexdec(substr($hexa, 1, 2));
			$c[] = hexdec(substr($hexa, 3, 2));
			$c[] = hexdec(substr($hexa, 5, 2));
			
			return $c;
		}
		
		function EcrireSousTitre($txt)
		{
			// Petit saut de ligne (sans perdre la position X)
			$this->SetY($this->GetY() + 3);
			
			if ($this->posXRef != 0) $this->SetX($this->posXRef);
			$c = $this->RenvoyerComposante($this->color_subtitle);
			$this->SetTextColor($c[0], $c[1], $c[2]);
			$this->SetFont("Arial", ""  ,12);
			$this->MultiCell(100, 7,	$this->WriteHtml(utf8_decode($txt)), "", "L", 0);			
		}

		function EcrireTexte($txt, $color = 0, $bgcolor = -1, $larg=40)
		{
			if ($this->posXRef != 0) $this->SetX($this->posXRef);
			if ($color === 0) $c = $this->RenvoyerComposante($this->color);
			else $c = $this->RenvoyerComposante($color);
			if ($bgcolor === 0) $bgc = $this->RenvoyerComposante($this->color_background);
			else if ($bgcolor > 0) $bgc = $this->RenvoyerComposante($bgcolor);
			
			if ($bgc) $this->SetFillColor($bgc[0], $bgc[1], $bgc[2]);
			$this->SetTextColor($c[0], $c[1], $c[2]);
			$this->SetFont		("Arial", ""  ,10);
			
			$this->MultiCell($larg, 6,	$this->WriteHtml(utf8_decode($txt)), 0, "R", ($bgc ? 1 : 0));
		}
		function EcrireTexte2($txt, $color = 0, $bgcolor = -1, $larg=40, $haut=6)
		{
			if ($this->posXRef != 0) $this->SetX($this->posXRef);
			if ($color === 0) $c = $this->RenvoyerComposante($this->color);
			else $c = $this->RenvoyerComposante($color);
			if ($bgcolor === 0) $bgc = $this->RenvoyerComposante($this->color_background);
			else if ($bgcolor > 0) $bgc = $this->RenvoyerComposante($bgcolor);
			
			if ($bgc) $this->SetFillColor($bgc[0], $bgc[1], $bgc[2]);
			$this->SetTextColor($c[0], $c[1], $c[2]);
			$this->SetFont		("Arial", ""  ,10);
			
			$this->MultiCell($larg, $haut,	$this->WriteHtml(utf8_decode($txt)), 0, "L", ($bgc ? 1 : 0));
		}
		
		function WriteHtml($txt)
		{
			$txt = trim($txt);
			$txt = str_replace("</p>",   "\n", $txt); // Paragraphe = Saut de ligne
			$txt = str_replace("<br>",   "\n", $txt); // Saut de ligne
			$txt = str_replace("<br/>",  "\n", $txt); // Saut de ligne
			$txt = str_replace("<br />", "\n", $txt); // Saut de ligne
			$txt = str_replace("<br />", "\n", $txt); // Saut de ligne
			$txt = str_replace("&gt;",   ">", $txt); // Supé
			$txt = str_replace("&lt;",   "<", $txt); // Infé
			$txt = str_replace("&#xa0;", " ", $txt); // espace insécable
			$txt = str_replace("’",      "'", $txt); // apostrophe française non reconnue !
			$txt = str_replace("&trade;","(TM)", $txt); // TradeMark
//			$txt = str_replace("?",      "(TM)", $txt); // TradeMark
			
			$txt = html_entity_decode($txt);			

			$txt = strip_tags($txt, "<strong><em>");

			$t = eregi("<em>([^<>]*)</em>", $txt, $tab);
			
			if ($t) {
//				var_dump($t, $tab);
//				die($txt);
			}
			$txt = strip_tags($txt);
			return $txt;
		}
		
		// 0 = pas de référence
		function SetPositionXRef($pos=0) { $this->posXRef = $pos; }
		function SetPositionYRef($pos=0) { $this->posYRef = $pos; }
	}


	$sql			= "SELECT * FROM `adherents`
							WHERE actif = 1";
	$result		= mysql_query($sql) or die ("Requête lecture incorrecte ".mysql_error());
	//while ($tmp=mysql_fetch_assoc($result)) {var_dump("test");}
	
	
	$pdf = new lPDF	("P", "mm", "A4");

	// Changer les couleurs
	//$pdf->SetColor("#000000", "#$valeurs[color_texte]", "#$valeurs[bgcolor_texte]", "#$valeurs[bgcolor_titre]");

	while ($tmp=mysql_fetch_assoc($result)) {
	// 1er page
	$pdf->AddPage		();
//		$pdf->SetFont		("Arial", "B" ,16);
//		$pdf->Cell			(0, 10,		utf8_decode("LUKAS Formation"), 0, 1, "C");

		$pdf->SetFont		("Arial", "B"  ,18);
		$pdf->Cell			(0, 10,		$pdf->WriteHtml(utf8_decode("ANNUAIRE")), 0, 1, "R");

		$pdf->Image("./templates/images/logo-pdf.jpg", 10, 5, 0, 20);
		$pdf->SetY($pdf->GetY() + 3);
		
// Image(string file, float x, float y [, float w [, float h [, string type 
// MultiCell(float w, float h, string txt [, mixed border [, string align [, int fill]]])

		$y = $pdf->GetY();
// Colonne centrale gauche
		$pdf->SetPositionXRef(0);
		$pdf->SetY($y);
		
			$pdf->EcrireSousTitre($tmp["prenom"]." ".$tmp["nom"]." (".$tmp["civilite"].")");
			$pdf->SetY($pdf->GetY() + 3);
		
			$pdf->EcrireTexte('Inscrit le', 0, -1, 50);
			$pdf->EcrireTexte('Nom entreprise', 0, -1, 50);
			$pdf->EcrireTexte("Adresse site", 0, -1, 50);
			
			$pdf->EcrireTexte('Fonction au sein de MVSE', 0, -1, 50);
			$pdf->SetY($pdf->GetY() + 3);
		
	
		
			$pdf->EcrireTexte("Adresse", 0, -1, 50);
			
			$pdf->EcrireTexte("Ville", 0, -1, 50);
			$pdf->EcrireTexte("Téléphones", 0, -1, 50);
			
			$pdf->EcrireTexte("Email", 0, -1, 50);
			
			$pdf->EcrireTexte("Type de public", 0, -1, 50);
			$pdf->EcrireTexte('Activité', 0, -1, 50);
			$pdf->SetY($pdf->GetY() + 3);
			$mytestY = $pdf->GetY();
			
		
			

			
			$pdf->SetY($pdf->GetY() + 3);
			
			if (isset($tmp["image1"]) && !empty($tmp["image1"])){
			$image1 = "./filemanager/".$tmp["image1"];
			$pdf->Image($image1, 25, 195, 0, 0);
			}
				if (isset($tmp["image2"]) && !empty($tmp["image2"])){
			$image2 = "./filemanager/".$tmp["image2"];
			$pdf->Image($image2, 90, 195, 0, 0);
			}
		
			
			$pdf->SetY($pdf->GetY() + 3);
			
			// Colonne centrale droite
		$pdf->SetPositionXRef(65);
		$pdf->SetY($y);
		
		
			$pdf->SetY($pdf->GetY() + 13);
	
			$pdf->EcrireTexte2(date("d/m/Y", strtotime($tmp["date_inscription"])), 0, 0, 90);
			$pdf->EcrireTexte2($tmp["nomentreprise"], 0, 0, 90);
				$pdf->EcrireTexte2($tmp["adressesite"], 0, 0, 90);
 		
			$pdf->EcrireTexte2($tmp["fctmvse"], 0, 0, 90);
		
			$pdf->SetY($pdf->GetY() + 3);
		
			
			$adresse1 = $tmp["ad1"];
			if (isset($tmp["ad2"]) && !empty($tmp["ad2"])) {
				$adresse1 .= " ".$tmp["ad2"];
			}
		
			$pdf->EcrireTexte2($adresse1, 0, 0, 100);
			
			$pdf->EcrireTexte2($tmp["codepostal"]." ".$tmp["ville"], 0, 0, 100);
			$pdf->EcrireTexte2($tmp["tel1"]." ".$tmp["tel2"], 0, 0, 100);
			
			$pdf->EcrireTexte2($tmp["email"], 0, 0, 100);
		
			$pdf->EcrireTexte2($tmp["typepublic"], 0, 0, 100);
				$pdf->EcrireTexte2($tmp["activite"], 0, 0, 100);
			$pdf->SetY($pdf->GetY() + 3);
			//$pdf->EcrireTexte2("", 0, 0, 100, 36);
			
			//$pdf->SetY($pdf->GetY());
			$pdf->EcrireTexte2($tmp["presentationent"], 0, 0, 130);
			$monXTest = $pdf->GetY();
			$pdf->SetY($pdf->GetY()+3);
			$pdf->EcrireTexte2($tmp["colabo"], 0, 0, 130);
			$pdf->SetY($pdf->GetY() + 3);
			$pdf->SetPositionXRef(0);
			$pdf->SetY($mytestY);
			$pdf->EcrireTexte("Présentation entreprise", 0, -1, 50);
			$pdf->SetY($monXTest + 3);
			$pdf->EcrireTexte("Je recherche des collaborateurs ou partenaires", 0, -1, 50);
			$pdf->SetY($pdf->GetY() + 3);
			
 //Colonne droite
		$pdf->SetPositionXRef(160);
		$pdf->SetY($y);
	
		
		if (isset($tmp["photo"]) && !empty($tmp["photo"])){
			$photo = "./filemanager/".$tmp["photo"];
			$pdf->Image($photo, 160, 30, 30, 0);
		}


		
		
		}
	$pdf->Close			();
	$pdf->Output		();

/*

$tmpfile = "__tmp.tmp";
file_put_contents("./scripts/dompdf/tmp/$tmpfile", file_get_contents("http://lukas3/lukas/site_web-dev/formations.php?zoom=9"));

$url = "scripts/dompdf/dompdf.php?input_file=" . rawurlencode($tmpfile) .
       "&paper=letter&output_file=" . rawurlencode("My Fancy PDF.pdf");

header("Location: $url");

*/

?>
